<?php
namespace app\community\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\community\Controller;
use think\Db;

class CaseNature extends Controller
{
    use \app\community\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];
    protected function get_role(){
        $role = Db::name('community_role')->where(['isdelete'=>0,'ty_id'=>getTyId()])->field('id,name')->order('id')->select()?:[];
        $html = '';
        foreach ( $role as $v ) {
            $html .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
        }
        $this->view->assign("role",$html );

    }

    protected function filter(&$map)
    {
        if ($this->request->param("nature")) {
            $map['nature'] = ["like", "%" . $this->request->param("nature") . "%"];
        }

    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        $controller = $this->request->controller();

        if ($this->request->isAjax()) {
            // 插入
            $data = [
                'nature'=>$this->request->post('nature/s'),
                'number'=>$this->request->post('number/d',10),
                'json_str'=>$this->request->post('json_str/s'),
                'ty_id'=>getTyId()
            ];
            // 简单的直接使用db写入
            Db::startTrans();
            try {
                $model = Db::name($this->parseTable($controller));
                $ret = $model->insert($data);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();

                return ajax_return_adv_error($e->getMessage());
            }
            return ajax_return_adv('添加成功');
        } else {
            // 添加
            $this->get_role();
            $this->view->assign("json_arr", []);
            return $this->view->fetch(isset($this->template) ? $this->template : 'edit');
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();

        if ($this->request->isAjax()) {
            // 更新
            $id = $this->request->post('id/d');
            if (!$id) {
                return ajax_return_adv_error("缺少参数ID");
            }
            $data = [
                'nature'=>$this->request->post('nature/s'),
                'number'=>$this->request->post('number/d',10),
                'json_str'=>$this->request->post('json_str/s'),
                'ty_id'=>getTyId()
            ];

            Db::startTrans();
            try {
                Db::name($this->parseTable($controller))->where('id', $id)->update($data);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajax_return_adv_error($e->getMessage());
            }


            return ajax_return_adv("编辑成功");
        } else {
            // 编辑
            $id = $this->request->param('id');
            if (!$id) {
                throw new \HttpException(404, "缺少参数ID");
            }
            $vo = $this->getModel($controller)->where(['ty_id'=>getTyId(),'id'=>$id])->find();
            if (!$vo) {
                throw new \HttpException(404, '该记录不存在');
            }
            if( '' === $vo['json_str'] ) {
                $json_arr = [];
                $json_last = [];
            } else {
                $json_arr = json_decode($vo['json_str'],true);
                $json_last = array_pop($json_arr);

            }

            $this->view->assign("vo", $vo);
            $this->view->assign("json_arr", $json_arr);
            $this->view->assign("json_last", $json_last);
            $this->get_role();
            return $this->view->fetch();
        }
    }

}
